package com.zsd.android.camerax.view.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.core.app.ActivityCompat
import com.zsd.android.camerax.R

class TailorView(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
    View(context, attrs, defStyleAttr) {

    private val CORNER_LENGTH = 50
    private var CORNER_OFFET = 0.0f
    //边框的画笔
    private val mRectPaint: Paint
    //角框的画笔
    private val mCornerPaint: Paint
    //背景画笔
    private val mBackPaint: Paint
    private val mRectF: RectF
    private var pWidth: Int = 0
    private var pHeight: Int = 0
    private var mWidth: Int = 0
    private var mHeight: Int = 0
    private var preX: Float = 0.0f
    private var preY: Float = 0.0f
    private var moveMode: MoveMode = MoveMode.OUTSIDE
    private var backRectF: ArrayList<RectF> = ArrayList()

    constructor(context: Context?) : this(context, null, 0) {

    }

    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0) {

    }

    init {
        mRectPaint = Paint()
        mRectPaint.style = Paint.Style.STROKE
        mRectPaint.strokeWidth = 4.0f
        mRectPaint.color = ActivityCompat.getColor(context!!, R.color.colorAccent)

        mCornerPaint = Paint()
        mCornerPaint.style = Paint.Style.STROKE
        mCornerPaint.strokeWidth = 12.0f
        mCornerPaint.color = ActivityCompat.getColor(context!!, R.color.colorPrimaryDark)
        CORNER_OFFET = 12.0f / 2

        mBackPaint = Paint()
        mBackPaint.style = Paint.Style.FILL
        mBackPaint.color = ActivityCompat.getColor(context!!, R.color.colorPopupBg)
        mRectF = RectF()

        backRectF.add(RectF())
        backRectF.add(RectF())
        backRectF.add(RectF())
        backRectF.add(RectF())
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        pWidth = w
        pHeight = h

        mWidth = pWidth * 2 / 3
        mHeight = 400
        mRectF.left = (pWidth - mWidth) / 2.0f
        mRectF.right = pWidth - mRectF.left
        mRectF.top = pHeight / 2.0f - mHeight / 2
        mRectF.bottom = pHeight / 2.0f + mHeight / 2
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.drawRect(mRectF, mRectPaint)
        //左上角
        canvas?.drawLine(mRectF.left - CORNER_OFFET, mRectF.top, mRectF.left + CORNER_LENGTH, mRectF.top, mCornerPaint)
        canvas?.drawLine(mRectF.left, mRectF.top - CORNER_OFFET, mRectF.left, mRectF.top + CORNER_LENGTH, mCornerPaint)
        //右上角
        canvas?.drawLine(mRectF.right + CORNER_OFFET, mRectF.top, mRectF.right - CORNER_LENGTH, mRectF.top, mCornerPaint)
        canvas?.drawLine(mRectF.right, mRectF.top - CORNER_OFFET, mRectF.right, mRectF.top + CORNER_LENGTH, mCornerPaint)
        //左下角
        canvas?.drawLine(mRectF.left - CORNER_OFFET, mRectF.bottom, mRectF.left + CORNER_LENGTH, mRectF.bottom, mCornerPaint)
        canvas?.drawLine(mRectF.left, mRectF.bottom + CORNER_OFFET, mRectF.left, mRectF.bottom - CORNER_LENGTH, mCornerPaint)
        //右下角
        canvas?.drawLine(mRectF.right + CORNER_OFFET, mRectF.bottom, mRectF.right - CORNER_LENGTH, mRectF.bottom, mCornerPaint)
        canvas?.drawLine(mRectF.right, mRectF.bottom + CORNER_OFFET, mRectF.right, mRectF.bottom - CORNER_LENGTH, mCornerPaint)
        //背景
        setBackRectF(mRectF)
        for (rectF in backRectF) {
            canvas?.drawRect(rectF, mBackPaint)
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                preX = event.x
                preY = event.y
                moveMode = touchMode(preX, preY)
            }
            MotionEvent.ACTION_MOVE -> {
                when (moveMode) {
                    MoveMode.LEFT_TOP -> {
                        //左上
                        var translateX = event.x - preX
                        var translateY = event.y - preY
                        mRectF.left += translateX
                        mRectF.top += translateY
                        preX = event.x
                        preY = event.y
                        postInvalidate()
                    }
                    MoveMode.RIGHT_TOP -> {
                        //右上
                        var translateX = event.x - preX
                        var translateY = event.y - preY
                        mRectF.right += translateX
                        mRectF.top += translateY
                        preX = event.x
                        preY = event.y
                        postInvalidate()
                    }
                    MoveMode.LEFT_BOTTOM -> {
                        //左下
                        var translateX = event.x - preX
                        var translateY = event.y - preY
                        mRectF.left += translateX
                        mRectF.bottom += translateY
                        preX = event.x
                        preY = event.y
                        postInvalidate()
                    }
                    MoveMode.RIGHT_BOTTOM -> {
                        //右下
                        var translateX = event.x - preX
                        var translateY = event.y - preY
                        mRectF.right += translateX
                        mRectF.bottom += translateY
                        preX = event.x
                        preY = event.y
                        postInvalidate()
                    }
                    MoveMode.INSIDE -> {
                        //中间
                        var translateX = event.x - preX
                        var translateY = event.y - preY
                        mRectF.left += translateX
                        mRectF.right += translateX
                        mRectF.top += translateY
                        mRectF.bottom += translateY
                        preX = event.x
                        preY = event.y
                        postInvalidate()
                    }
                }
            }
            MotionEvent.ACTION_UP -> {
                moveMode = MoveMode.OUTSIDE
            }
        }
        return true
    }

    private fun setBackRectF(mRectF: RectF) {
        for (i in 0 .. backRectF.size - 1) {
            var rectF = backRectF.get(i)
            if (0 == i) {
                rectF.left = 0.0f
                rectF.right = pWidth * 1.0f
                rectF.top = 0.0f
                rectF.bottom = mRectF.top
            } else if (1 == i) {
                rectF.left = 0.0f
                rectF.right = mRectF.left
                rectF.top = mRectF.top
                rectF.bottom = mRectF.bottom
            } else if (2 == i) {
                rectF.left = mRectF.right
                rectF.right = pWidth * 1.0f
                rectF.top = mRectF.top
                rectF.bottom = mRectF.bottom
            } else if (3 == i) {
                rectF.left = 0.0f
                rectF.right = pWidth * 1.0f
                rectF.top = mRectF.bottom
                rectF.bottom = pHeight * 1.0f
            }
        }
    }

    private fun touchMode(preX: Float, preY: Float) : MoveMode {
        if (touchLeftTop(preX, preY)) {
            return MoveMode.LEFT_TOP
        } else if (touchRightTop(preX, preY)) {
            return MoveMode.RIGHT_TOP
        } else if (touchLeftBottom(preX, preY)) {
            return MoveMode.LEFT_BOTTOM
        } else if (touchRightBottom(preX, preY)) {
            return MoveMode.RIGHT_BOTTOM
        } else if (touchInside(preX, preY)) {
            return MoveMode.INSIDE
        }
        return MoveMode.OUTSIDE
    }

    /**
     * 判断是否点击在框内部
     */
    private fun touchInside(preX: Float, preY: Float) : Boolean {
        return mRectF.contains(preX, preY)
    }

    /**
     * 判断是否点击在左上角
     */
    private fun touchLeftTop(preX: Float, preY: Float) : Boolean {
        if (preX >= mRectF.left && preX <= mRectF.left + CORNER_LENGTH &&
                preY >= mRectF.top && preY <= mRectF.top + CORNER_LENGTH) {
            return true
        }
        return false
    }

    /**
     * 判断是否点击在右上角
     */
    private fun touchRightTop(preX: Float, preY: Float) : Boolean {
        if (preX >= mRectF.right - CORNER_LENGTH && preX <= mRectF.right &&
            preY >= mRectF.top && preY <= mRectF.top + CORNER_LENGTH) {
            return true
        }
        return false
    }

    /**
     * 判断是否点击在左下角
     */
    private fun touchLeftBottom(preX: Float, preY: Float) : Boolean {
        if (preX >= mRectF.left && preX <= mRectF.left + CORNER_LENGTH &&
            preY >= mRectF.bottom - CORNER_LENGTH && preY <= mRectF.bottom) {
            return true
        }
        return false
    }

    /**
     * 判断是否点击在右下角
     */
    private fun touchRightBottom(preX: Float, preY: Float) : Boolean {
        if (preX >= mRectF.right - CORNER_LENGTH && preX <= mRectF.right &&
            preY >= mRectF.bottom - CORNER_LENGTH && preY <= mRectF.bottom) {
            return true
        }
        return false
    }

    enum class MoveMode {
        LEFT,
        TOP,
        RIGHT,
        BOTTOM,
        LEFT_TOP,
        RIGHT_TOP,
        LEFT_BOTTOM,
        RIGHT_BOTTOM,
        INSIDE,
        OUTSIDE
    }
}